<template>
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="banner in list" :key="banner.id">
        <img :src="banner.img" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
  name: 'Carousel',
  props:['list'],
  watch: {
    list: {
      handler(newVal, oldVal) {
        if (!newVal.length) return //防止生成swiper实例多次 把立即执行这次去除掉
        // watch + $nextTick watch能保证数据变了 $nextTick能保证这次页面更新了
        //等到这次页面更新完成 才会执行里面的回调函数
        this.$nextTick(() => {
          new Swiper(this.$refs.mySwiper, {
            loop: true, //无缝轮播
            speed: 2000, //切换时间  到下一张所需要的时间
            spaceBetween: 0, //每屏间隔大小
            effect:'fade',
            autoplay: {
              //自动轮播
              delay: 2000, //延迟时间   当前屏显示多久 去下一屏
              disableOnInteraction: false, //控制鼠标操作之后 还自动轮播不
            },
            pagination: {
              el: '.swiper-pagination', //获取小圆点的容器
              clickable: true, //是否可以点击小圆点
            },
            navigation: {
              //获取左右按钮容器
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev',
            },
          })
        })
      },
      immediate: true, //为了和floor一致
    },
  },
}
</script>

<style lang="less" scoped>
.swiper-slide img{
  width: 1150px;
  margin-left: 10px;
  margin-top: 10px;
}
</style>
